package com.it.base.thread.pool.newSingleThreadExecutorDemo;

import com.it.base.thread.pool.newSingleThreadExecutorDemo.Task.XxxService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 创建一个单线程化的线程池，它只会用唯一的工作线程来执行任务，保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
 */
public class MyNewSingleThreadExecutor {
    private static ExecutorService executorService=   Executors.newSingleThreadExecutor();

    /**
     *  提交到线程池中，对原生提交任务进行封装
     * @param runnable
     */
    public static Future submit(Runnable runnable){
        Future future= executorService.submit(runnable);
        return future;
    }

    public static void main(String[] args) {
        // 创建两个处理的数据集合
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for(int i = 0;i<10000;i++){
            list1.add(i+1);
            list2.add(i+1);
        }
        XxxService xxxService = new XxxService(list1);
        MyNewSingleThreadExecutor.submit(xxxService);

        XxxService xxxService2 = new XxxService(list2);
        MyNewSingleThreadExecutor.submit(xxxService2);
    }
}
